<?php

namespace app\langjinmobile\controller;
//导入基类
use think\Controller;
use think\facade\Cache;
use think\Db;
use app\common\model\Shoporder as shoporderModel;
use app\common\model\Business as businessModel;
use app\common\model\User as userrModel;
use app\common\model\Goods as goodsModel;
use app\common\model\Cart as cartModel;
use think\Request;
use think\Validate;
use app\common\model\Address as addressModel;
use app\common\model\UserLevel as userLevelModel;
use app\common\model\Money as moneyModel;
use app\common\model\ShopConfig;

class Card extends Controller 
{

	public function order(){
		$status = request()->param('status');
		//查找已付款订单 status=1
        $id = session('userid');
        $orders = shoporderModel::alias('a')
        ->Join('langjin_goods w','a.goods_id = w.id')
        ->Join('langjin_business z','a.shops_id = z.id')
        ->where('a.userid',$id)
        ->where('a.status',$status)
        ->field(['a.dprice','a.goods_id','a.order_id','a.tprice','a.num','a.order_number','a.wallet_num','a.integral_num','w.name','w.img1','z.name shop_name'])
        ->order('order_number')
        ->select();
        //总价
        $total = shoporderModel::where('userid',$id)->where('status',$status)
        ->field('SUM(tprice) total')
        ->group('order_number')
        ->order('order_number')
        ->select();
        //总件数
        $nums = shoporderModel::where('userid',$id)->where('status',$status)
        ->field('SUM(num) nums')
        ->group('order_number')
        ->order('order_number')
        ->select();
        //单号
        $order_nos = shoporderModel::where('userid',$id)->where('status',$status)
        ->field('order_id, order_number')
        ->group('order_number')
        ->order('order_number')
        ->select();
        //订单编号一样为一个数组
        foreach ($orders as $k=>$v) {
            $order[$v['order_number']][$v['shop_name']][] = $v;
        }
        // dump($orders);exit;
        if(!empty($order)){
          $order = array_values($order);//下标改为从0开始
          foreach ($order as $key => $value) {
            foreach($value as $k=>$v){
                foreach ($v as $kk => $vv) {
                    $img = explode(',',$vv['img1']);
                    $v[$kk]['img1'] = $img[0];

                }

            }
          }

        }else{
          $order = '';
        }
        // dump($nums);exit;
        $this->assign('order',$order);
        $this->assign('total',$total);
        $this->assign('nums',$nums);
        $this->assign('order_nos',$order_nos);
        $this->assign('status',$status);
        return $this->fetch();
	}

	//购物车
	public function shopcard(){
		
		$uid=Session('userid');
        $datas=cartModel::where(['userid'=>$uid,'status'=>1])->order('update_time','desc')->select();
        foreach($datas as $k=>$v){
            $info=goodsModel::where(['id'=>$v['goods_id']])->find();
            $img = explode(',',$info['img1']);
            $datas[$k]['img1'] = $img[0];
            $datas[$k]['good_name'] = $info['name'];
            $datas[$k]['wallet_num'] = $info['wallet_num'];
            $datas[$k]['integral_num'] = $info['integral_num'];
        }
        return $this->fetch('',['datas'=>$datas]);
	}

	//商品列表
	public function goodsdata(Request $request){

		$good_id =$request->param('good_id');
        //商品列表
        $goods_list = goodsModel::where('id',$good_id)->find();
        $goods_list['img1'] = explode(',',$goods_list['img1']);
        $goods_list['detail'] = htmlspecialchars_decode($goods_list['detail']);
        $mobile = businessModel::where('id',$goods_list['bis_id'])->field('mobile')->find();
        $goods_list['mobile'] = $mobile['mobile'];
        $record = shoporderModel::where('goods_id',$good_id)->order('create_time desc')->select()->toArray();
        foreach($record as $key=>$value){
            $user_name = userrModel::where('id',$value['userid'])->field('nickname')->find();
            $record[$key]['user_name'] = $user_name['nickname'];
        }
        //print_r($record);exit;
        $this->assign('goods_list',$goods_list);
        $this->assign('record',$record);
		return $this->fetch(); 
	}
    //商品列表
    public function goodsinfo(Request $request){

        $level_id =$request->param('level_id');
        //商品列表
        $goods_list = goodsModel::where(['bis_id'=>0,'lev'=>$level_id])->find();
        if(empty($goods_list)){
           echo "<script>alert('获取数据失败');window.location.href= '/langjinmobile/home/index';</script>";exit;
        }
        $goods_list['img1'] = explode(',',$goods_list['img1']);
        $goods_list['detail'] = htmlspecialchars_decode($goods_list['detail']);
        $mobile = businessModel::where('id',$goods_list['bis_id'])->field('mobile')->find();
        $goods_list['mobile'] = $mobile['mobile'];
        $record = shoporderModel::where('goods_id',$goods_list['id'])->order('create_time desc')->select()->toArray();
        foreach($record as $key=>$value){
            $user_name = userrModel::where('id',$value['userid'])->field('nickname')->find();
            $record[$key]['user_name'] = $user_name['nickname'];
        }
        //print_r($record);exit;
        $this->assign('goods_list',$goods_list);
        $this->assign('record',$record);
        return $this->fetch('goodsdata');
    }
	public function tenants(){
		return $this->fetch(); 
    }
    


    public function distr(){
		return $this->fetch(); 
    }

    public function paycom(){
		return $this->fetch(); 
    }
    
    

	 /**
     * 添加到购物车
     */

    public function addcart()
    {
        //商品 店铺 数量  
        $uid=Session('userid');
        if(request()->isAjax()){
            
                $goodid=input('good_id');//商品id
            
                $num=input('num');//购买数量
                if($num<=0){
                	return array('status'=>'2','msg'=>'数量错误');
                }
                //商品详情
                $goodsdata=goodsModel::where('id',$goodid)->find();
                if(empty($goodsdata) || !$goodsdata){   
                    return $msg=['status'=>2,'msg'=>lang('该商品不存在')];
                }
                $bis_id=$goodsdata['bis_id'];//商铺id
                // 判断数量 库存
                $validate = Validate::make(['num'=>"number|between:1,{$goodsdata['amount']}"], [], ['num'=>'库存不足']);
                if(!$validate->check(input('post.'))){
                    return $msg=['status'=>2,'msg'=>lang('库存不足')];
                };
                //判断购物车是否已存在该商品
                $res=cartModel::where(['userid'=>$uid,'status'=>1,'goods_id'=>$goodid])->find();
               
                // 启动事务
                Db::startTrans();
                try {
                    //已存在增加数量
                    if($res){ 
                        $newnum=$res['num']+$num;
                        if(bccomp($newnum,$goodsdata['amount'])>0){
                            return $msg=['status'=>2,'msg'=>lang('库存不足')];
                        }
                        $newtprice=bcmul($newnum,$res['dprice'],4);
                        cartModel::where(['car_id'=>$res['car_id'],'goods_id'=>$goodid,'status'=>1,'userid'=>$uid])->update(['num'=>$newnum,'tprice'=>$newtprice,'update_time'=>time()]);
                    }else{
                        $data=[
                            'shops_id'=>$goodsdata['bis_id'],
                            'goods_id'=>$goodid,
                            'userid'=>$uid,
                            'num'=>$num,
                            'dprice'=>$goodsdata['sale_price'],
                            'tprice'=>bcmul($goodsdata['sale_price'],$num,4),
                            'status'=>1,
                            // 'create_time'=>time(),
                            // 'update_time'=>time(),
                        ];
                        $cartModel=new cartModel;
                        $cartModel->save($data);
                    }
                    
                    // 提交事务
                    Db::commit();
                    return $msg=['status'=>1];
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    echo $e;
                    return $msg=['status'=>2];
                }
           
        }
        
    }


    /**
     * 确认订单
     */
    public function makesure()
    {
        $uid=session('userid');
        
        if(request()->isAjax()){
            $car_id=input('post.ids');//选中订单
            $car_info=input('post.info');//选中详情
            
            if(!$car_info['0']){
              
                $this->error('购物车为空！');
            }
            //dump($car_info);die('aaaa');
            // 启动事务
            Db::startTrans();
            try {
                foreach($car_id as $k=>$v){
                    $orderinfo=cartModel::where(['userid'=>$uid,'status'=>1,'car_id'=>$v])->find();
                    //$newtprice=$v['num']*$orderinfo['dprice'];
                    //判断库存
                    $goodsdata=goodsModel::where('id',$orderinfo['goods_id'])->find();
                    $n = $car_info[$k];
                    if($car_info[$k]>$goodsdata['amount']){
                        return $msg=['status'=>2,'msg'=>'商品库存不足'];
                    }
                    cartModel::where(['userid'=>$uid,'status'=>1,'car_id'=>$v])->update(['num'=>$n]);
                }
            // 提交事务
            Db::commit();
            session('shoporder',$car_id);//存入确认订单
            return $msg=['status'=>1];
           } catch (\Exception $e) {
               // 回滚事务
               Db::rollback();
               
               return $msg=['status'=>2,'msg'=>'结算数量错误'];
           }
        }
        $car_id= session('shoporder');
        //选中的商品 查询有几家店铺
        $res= cartModel::where(['userid'=>$uid,'status'=>1])->where('car_id','in',$car_id)->distinct(true)->field('shops_id')->select();
       $res1 = cartModel::where(['userid'=>$uid,'status'=>1])->where('car_id','in',$car_id)->distinct(true)->field('shops_id')->select();
       //件数
        $tnum=cartModel::where(['userid'=>$uid,'status'=>1])->where('car_id','in',$car_id)->sum('num');
       //总价
        $totprice=cartModel::where(['userid'=>$uid,'status'=>1])->where('car_id','in',$car_id)->sum('tprice');
        if(!$res || !$tnum ){
        	
             $this->error('购物车为空！');
        }
        //循环
        foreach($res as $k=>$v){
           $v['goods']= cartModel::where(['userid'=>$uid,'status'=>1,'shops_id'=>$v->getData('shops_id')])->where('car_id','in',$car_id)->select();
           // print_r($v['goods']);exit;
            foreach($v['goods'] as $k1=>$v1){
                $info=goodsModel::where(['id'=>$v1['goods_id']])->find();
                $img = explode(',',$info['img1']);
                $v['goods'][$k1]['img1'] = $img[0];
                $v['goods'][$k1]['good_name'] = $info['name'];
                $v['goods'][$k1]['wallet_num'] = $info['wallet_num'];
                $v['goods'][$k1]['integral_num'] = $info['integral_num'];
                // $v['goods'][$k1]['num'] = $
            }
        }//dump($res1);die('dsss');
        //收货详情
        $address=addressModel::where(['userid'=>$uid,'attribute'=>1])->find();

        return $this->fetch('',['res'=>$res,'tnum'=>$tnum,'totprice'=>$totprice,'address'=>$address]);
    }

	public function map(){
		return $this->fetch(); 
	}

	 /**
     * 支付页面 平台自营
     */

    public function zhipay()
    {
         //商品 店铺 数量  
         $uid=Session('userid');
         $info = request()->param();

         if(request()->isAjax()){
            $userinfo = userrModel::where('id',$uid)->find();
            //判断支付密码
            if(!encrypt($uid, input('post.pwd'))){
                return $msg=['status'=>2,'msg'=>lang('支付密码错误！')];
            }
           //获取商品价格
            $goods = goodsModel::where('id',$info['good_id'])->find();
            if(empty($goods)){
            	return array('status'=>'2','msg'=>'购买异常');
            }
            //查询会员等级 必须购买大于当前会员等级1的产品
            $user_level = DB::table('langjin_user_level')->where('userid',$uid)->find();
            if($user_level['user_grade'] == 9){
            	return array('status'=>'2','msg'=>'您已经是最高星级了');
            }
            $grade = $user_level['user_grade'] +1;
            if($goods['lev'] != $grade){
            	return array('status'=>'2','msg'=>'请购买'.$grade.'星会员');
            }
            
            //查询GMOBGMOD是否充足
            $money = moneyModel::where('userid',$uid)->find();
            
            
            if(bccomp($money['balance'],$goods['wallet_num'],4) < 0){
            	return array('status'=>'2','msg'=>'GMOB不足');
            }
            if(bccomp($money['integral'],$goods['integral_num'],4) < 0){
            	return array('status'=>'2','msg'=>'GMOD不足');
            }

            // 启动事务
			Db::startTrans();
			try {
				//修改用户等级
				DB::table('langjin_user_level')->where('userid',$uid)->update(['user_grade'=>$grade]);
			    //扣除GMOB 扣除GMOD
	            $ywallet=$this->getwalletnum($uid); 
	            moneyModel::where('userid',$uid)->setDec('balance',$goods['wallet_num']);
	            $nwallet=$this->getwalletnum($uid);
	            $yinter = $this->getinternum($uid);
	            moneyModel::where('userid',$uid)->setDec('integral',$goods['integral_num']);
	            $ninter = $this->getinternum($uid);
	            //直推,辅导奖
	            cengji($uid,$goods['integral_num'],$goods['wallet_num']);
	            //团队业绩
	            team_integral($uid,$goods['integral_num']);
	            //加权分红
	            dividend($uid,$goods['integral_num']);
	            //点位奖
	            make_point($uid,$goods['integral_num'],$goods['lev']);


	            //生成订单单号
	            $order_number=$this->createordernum();
	            $data=array();
	                    $data=[
	                        'order_number'=>$order_number,
	                        'shops_id'=>0,
	                        'goods_id'=>$goods['id'],
	                        'userid'=>$uid,
	                        'num'=>'1',
	                        'dprice'=>'1',//自营商品不需要
	                        'tprice'=>'1',//自营商品不需要
	                        'address'=>'1',//自营不需要
	                        // 'address'   =>  $address['id'],
	                        'status'=>3,
	                        'poundage'=>0,
	                        'mobile'=>$userinfo['mobile'],
	                        'username'=>$userinfo['nickname'],
	                        'note'=>'购买已完成',
	                        'wallet_num'=>$goods['wallet_num'],
	                        'integral_num'=>$goods['integral_num']
	                    ];
	            $shoporderModel=new shoporderModel;
	            $res = $shoporderModel->save($data);
	            //生成GMOB记录 
	            $datag=[
	                    'userid'=>$uid,
	                    'money'=>'-'.$goods['wallet_num'],
	                    'type'=>8,//商品购买扣除
	                    'before_wallet'=>$ywallet,
	                    'now_wallet'=>$nwallet,
	                    'create_time'=>time(),
	                ];
	            Db::table('langjin_wallet_log')->insert($datag);
	            //生成GMOD记录
	            //langjin_integral_log
	            $datag1=[
	                    'userid'=>$uid,
	                    'num'=>'-'.$goods['integral_num'],
	                    'type'=>13,//商品购买扣除
	                    'before_integral'=>$yinter,
	                    'now_integral'=>$ninter,
	                    'create_time'=>time(),
	                ];
	            Db::table('langjin_integral_log')->insert($datag1);
			    // 提交事务
			    Db::commit();
			    return array('status'=>'1','msg'=>'购买成功');
			} catch (\Exception $e) {
			    // 回滚事务
			    Db::rollback();
			    return array('status'=>'2','msg'=>'购买失败');
			}
            
           }
          
    }
   

    //生成唯一订单号
    public function createordernum()
    {   
        $order_number=mt_rand(100000,999999).substr(microtime(), 2, 5).date('YmdHis');
        //判断是否已存在
        $result=shoporderModel::where('order_number',$order_number)->find();
        if(!$result){
            return $order_number;
        }else{
            
            return $this->createordernum();
        }
       
    }



    //获取GMOB值
    public function getwalletnum($uid){
       //langjin_integral_log
        $datas=Db::table('langjin_money')->where(array('userid'=>$uid))->find();
        if($datas){
            return $datas['balance'];
        }
       
    }

    //获取GMOB值
    public function getinternum($uid){
       //langjin_integral_log
        $datas=Db::table('langjin_money')->where(array('userid'=>$uid))->find();
        if($datas){
            return $datas['integral'];
        }
       
    }


    //判断收货地址是否存在  同时生成订单信息
    public function saveaddress()
    {
        $uid=Session('userid');
        //收货详情
        $address=addressModel::where(['userid'=>$uid,'attribute'=>1,'id'=>input('post.addressid')])->find();
        $ids=session('shoporder');
        session('note',input('post.note'));
        
        if($address && $ids){
            return $msg=['status'=>1];
        }else{
            return $msg=['status'=>2];
        }
    }





     /**
     * 支付页面
     */

    public function zhiok()
    {
         //商品 店铺 数量  
         $uid=Session('userid');
       //收货详情
        $address=addressModel::where(['userid'=>$uid,'attribute'=>1])->find();
        if(!$address){
        	
            $this->error('收货地址为空！');
          die;
        }
        if(!session('shoporder')){
        	
            $this->error('购物车为空！');
         }
         if(request()->isAjax()){
            
            //判断支付密码
            if(!encrypt($uid, input('post.pwd'))){
                return $msg=['status'=>2,'msg'=>lang('支付密码错误！')];
            }
           
            // 启动事务
            Db::startTrans();
            try {
                //生成订单单号
                $car_id=session('shoporder');//存入确认订单
                if(!$car_id){
                    return $msg=['status'=>2,'msg'=>lang('商品数量错误！')];
                }
                //查询手续费 设置
                $poundage=ShopConfig::where(['id'=>5])->find();
                $sellerintegral=ShopConfig::where(['id'=>8])->find();
                $sellermoney=ShopConfig::where(['id'=>9])->find();
                $seller_giveintegral=ShopConfig::where(['id'=>6])->find();
                $platform_giveintegral=ShopConfig::where(['id'=>7])->find();
                //循环加入
                $ids = '';
                foreach($car_id as $k=>$v){
                    $order_number=$this->createordernum();
                    //购物订单查询
                    $cartorder=cartModel::where(['car_id'=>$v,'userid'=>$uid])->find();
                    //查询商品价格
                    $gps = goodsModel::where('id',$cartorder['goods_id'])->find();
                    $ids .= $gps['id'].','; 
                    $wall[] =  $cartorder['num']*$gps['wallet_num'];
                    $inter[] = $cartorder['num']*$gps['integral_num'];
                    $data=array();
                    $data=[
                        'order_number'=>$order_number,
                        'shops_id'=>$cartorder['shops_id'],
                        'goods_id'=>$cartorder['goods_id'],
                        'userid'=>$cartorder['userid'],
                        'num'=>$cartorder['num'],
                        'dprice'=>$cartorder['dprice'],
                        'tprice'=>$cartorder['tprice'],
                        'address'=>$address['province'].$address['city'].$address['area'].$address['street'].$address['address'],
                        // 'address'   =>  $address['id'],
                        'status'=>1,
                        'poundage'=>$poundage['num'],
                        'seller_getintegral'=>$sellerintegral['num'],
                        'seller_getmoney'=>$sellermoney['num'],
                        'seller_giveintegral'=>$seller_giveintegral['num'],
                        'platform_giveintegral'=>$platform_giveintegral['num'],
                        'mobile'=>$address['mobile'],
                        'username'=>$address['username'],
                        'note'=>session('note'),
                        'wallet_num'=>$gps['wallet_num'],
                        'integral_num'=>$gps['integral_num']
                    ];
                    $shoporderModel=new shoporderModel;
                    $shoporderModel->save($data);
                    
                    goodsModel::where(['id'=>$cartorder['goods_id']])->setInc('sales',$cartorder['num']);
                    goodsModel::where(['id'=>$cartorder['goods_id']])->setDec('amount',$cartorder['num']);
                    //判断库存 为0下架
                    $amount=goodsModel::where(['id'=>$cartorder['goods_id']])->find();
                    if($amount['amount']<=0){
                        $amount->save(['is_on'=>0]);
                    }

                }
                $wall_num = array_sum($wall);
                $inter_num = array_sum($inter);
                
                //总价
                //$totprice=cartModel::where(['userid'=>$uid,'status'=>1])->where('car_id','in',$car_id)->sum('tprice');
                cartModel::where(['car_id'=>$car_id,'userid'=>$uid])->update(['status'=>2]);
                //扣除 判断GMOB数量
                $uidinfo=moneyModel::where(['userid'=>$uid])->find();
                if(bccomp($uidinfo['balance'],$wall_num,4)<0){
                    return $msg=['status'=>2,'msg'=>lang('GMOB不足')];
                }
                if(bccomp($uidinfo['integral'],$inter_num,4)<0){
                    return $msg=['status'=>2,'msg'=>lang('GMOD不足')];
                }
                //扣除GMOB 扣除GMOD
	            $ywallet=$this->getwalletnum($uid); 
	            moneyModel::where('userid',$uid)->setDec('balance',$wall_num);
	            $nwallet=$this->getwalletnum($uid);
	            $yinter = $this->getinternum($uid);
	            moneyModel::where('userid',$uid)->setDec('integral',$inter_num);
	            $ninter = $this->getinternum($uid);
                //生成GMOB记录 
	            $datag=[
	                    'userid'=>$uid,
	                    'money'=>'-'.$wall_num,
	                    'type'=>8,//商品购买扣除
	                    'before_wallet'=>$ywallet,
	                    'now_wallet'=>$nwallet,
	                    'create_time'=>time(),
	                ];
	            Db::table('langjin_wallet_log')->insert($datag);
	            //生成GMOD记录
	            //langjin_integral_log
	            $datag1=[
	                    'userid'=>$uid,
	                    'num'=>'-'.$inter_num,
	                    'type'=>13,//商品购买扣除
	                    'before_integral'=>$yinter,
	                    'now_integral'=>$ninter,
	                    'create_time'=>time(),
	                ];
	            Db::table('langjin_integral_log')->insert($datag1);
                // 提交事务
                Db::commit();
                session('shoporder',null);
                session('note',null);
                return $msg=['status'=>1];
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                echo $e;
                return $msg=['status'=>2];
            }
            
         }
         $car_id= session('shoporder');
        
        //选中的商品 查询有几家店铺
        $res= cartModel::where(['userid'=>$uid,'status'=>1])->where('car_id','in',$car_id)->distinct(true)->field('shops_id')->select();
        
       //件数
        $tnum=cartModel::where(['userid'=>$uid,'status'=>1])->where('car_id','in',$car_id)->sum('num');
       //总价
        $totprice=cartModel::where(['userid'=>$uid,'status'=>1])->where('car_id','in',$car_id)->sum('tprice');
        if(!$res || !$tnum ){
            $this->error('购物车为空！');
        }
        //循环
        foreach($res as $k=>$v){
           $v['goods']= cartModel::where(['userid'=>$uid,'status'=>1,'shops_id'=>$v->getData('shops_id')])->where('car_id','in',$car_id)->select();
            foreach($v['goods'] as $k1=>$v1){
                $info=goodsModel::where(['id'=>$v1['goods_id']])->find();
                $img = explode(',',$info['img1']);
                $v['goods'][$k1]['img1'] = $img[0];
                $v['goods'][$k1]['good_name'] = $info['name'];
                $v['goods'][$k1]['wallet_num'] = $info['wallet_num'];
                $v['goods'][$k1]['integral_num'] = $info['integral_num'];
            }
        }
        //收货详情
        $address=addressModel::where(['userid'=>$uid,'attribute'=>1])->find();
        return $this->fetch('',['res'=>$res,'tnum'=>$tnum,'totprice'=>$totprice,'address'=>$address]);
    }


    //删除购物商品

    public function deletecart()
    {
        //商品 店铺 数量  
        $uid=Session('userid');
        if(request()->isAjax()){
           $car_id=input('post.Oid');//购物车id
           //判断
           $res=cartModel::where(['userid'=>$uid,'status'=>1])->where('car_id','in',$car_id)->select();
           if(!$res || empty($res)){
                return $msg=['status'=>2,'msg'=>lang('该订单不存在')];
           }
           
            // 启动事务
           Db::startTrans();
            try {
                $data=[
                    'status'=>3,
                    'update_time'=>time()
                ];
                cartModel::where(['userid'=>$uid,'status'=>1])->where('car_id','in',$car_id)->update($data);
                // 提交事务
                Db::commit();
                return $msg=['status'=>1,'msg'=>lang('删除成功')];
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return $msg=['status'=>2,'msg'=>lang('删除失败')];
            }
           
        }
        
    }


    /**
    * 确认收货
    */
    public function delivery()
    {
        Db::startTrans();
        try{
                $id = session('userid');
                //获取订单编号
                $num = input('post.num');
                //查询订单
                $orders = shoporderModel::where('order_id',$num)->find();
                if($orders['status'] == '3'){
                	return array('code'=>0,'msg'=>lang('订单已确认'));
                }
                $busisness = businessModel::where('id',$orders['shops_id'])->find();
                $n = $orders['num'];
                //总GMOB 总GMOD
                $wall_total = $n*$orders['wallet_num'];
                $inter_total = $n*$orders['integral_num'];
                $data['status'] = 3;
                $data['update_time'] = time();
                $order_number = shoporderModel::where(['order_id'=>$num, 'status'=>2])->update($data);
                //商户返钱
                $ztprice = Db::table('langjin_shoporder')->where(['order_number'=>$num])->sum('tprice');//总钱数
                $shopid =  shoporderModel::where(['order_number'=>$num])->select();//商户id
                $wall = moneyModel::where('userid',$busisness['userid'])->find();
                //卖家GMOB增加
                //$busisness['userid']
                $array = array(
                        'userid' => $busisness['userid'],
                        'money' => $wall_total,
                        'type' => 9,
                        'before_wallet' => $wall['balance'],
                        'now_wallet' => $wall['balance']+$wall_total,
                        'create_time'=>time()
                    );
                	Db::table('langjin_wallet_log')->insert($array);
                    //$result = walletLogModel::create($array);
                //卖家GMOD增加
                //$busisness['userid']
                $dataintegrallogs=[
                        'userid' => $busisness['userid'],
                        'num' => $inter_total,
                        'type' => 14,
                        'before_integral' => $wall['integral'],
                        'now_integral' => $wall['integral']+$inter_total,
                        'create_time'=>time()
                        ];
                Db::table('langjin_integral_log')->insert($dataintegrallogs);
                    //integralLogModel::create($dataintegrallogs); 


                //die('dsss');
               

                // 提交事务
                Db::commit();
                return array('code'=>1,'msg'=>lang('确认收货成功')); 
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
             //   echo $e;
                return array('code'=>0,'msg'=>lang('确认收货失败'));
            }
    }









}